<?php

class Member{

	/**
	 * 添加
	 *
	 * @param unknown_type $name
	 * @param unknown_type $parent
	 * @param unknown_type $ord
	 */
	function add($data){
		
		$data['created'] = full_date();
    	$data['admin_id'] = admin_id_ajax();
    	$data['last_use_time'] = '1970-01-01 00:00:00';
    	
		$m = M('Member');
		//返回自增id
		$id = $m->add($data);
		
		return $id;
	}
	
	/**
	 * 修改
	 *
	 * @param unknown_type $id
	 * @param unknown_type $upItems
	 * @return unknown
	 */
	function update($id,$upItems){
		$m = M('Member');
		return $m -> where('member_id='.$id)->save($upItems);
	}
	
	function get_by_member_id($id){
		$m = M('Member');
		return $m -> getByMemberId($id);
	}

	function lists($where,$page,$pageSize,&$allCount){

        $whereSql = ' 1=1 ';

        if(!is_null($where)){

            $where['end_time'] = short_date(strtotime($where['end_time']) + 86400);

            //查询条件
            $whereSql .= " AND (m.created BETWEEN '{$where['begin_time']}' AND '{$where['end_time']}') ";

            if($where['plantform_id'] > 0){
                $whereSql .= " AND m.plantform_id = {$where['plantform_id']} ";
            }

            if($where['search']){

                $item = $where['search']['item'];
                $value = $where['search']['value'];

                if($item != '' && $value != ''){
                    $whereSql .= " AND m.{$item} LIKE '%{$value}%' ";
                }
            }
        }

		//开始行
		$startIndex = ($page-1) * $pageSize;
		
		//总计数
		$allCount = $this->lists_count($whereSql);

		$m = M('Member');
		return $m->table('member m')
		->join('admin a ON a.id = m.admin_id')
		->join('plantform p ON p.plantform_id = m.plantform_id')
        ->where($whereSql)
		->order('member_id DESC')
		->limit($startIndex. ',' . $pageSize)
		->field('m.*,p.plantform_name,a.real_name')
		->select();
	}

	function lists_count($whereSql){

		$m = M('Member');
		return $m->table('member m')
		->join('admin a ON a.id = m.admin_id')
		->join('plantform p ON p.plantform_id = m.plantform_id')
        ->where($whereSql)
		->getField('COUNT(*)');
	}
	
	/**
	 * 
	 * 按平台获取最早未使用过的会员
	 * @param unknown_type $plantform_id
	 */
	function get_by_plantform_id($plantform_id,$number =null,$exclude_members=array()){
		
		if(is_null($number)){
			$number = PHP_INT_MAX;
		}

		//排除的会员
		$extSql = '';
		if(!empty($exclude_members)){
			$ms = implode(',', $exclude_members);
			$extSql = " AND member_id NOT IN ($ms)";
		}

		$m = M('Member');
		return $m->table('member')
		->where('disabled = 0 AND plantform_id='.$plantform_id.$extSql)
		->order('last_use_time ASC')
		->limit('0,' . $number)
		->select();
	}
	
	
	/**
	 * 
	 * 返回平台
	 * @param $members
	 */
	function append_plantform_id($members){
	
		
		import('@.ORG.Plantform');
		
		$cPlantform = new Plantform();
		
		$maps = $cPlantform->maps();
		
		foreach ($members as $i => $m){
			
			$plantform_id = 0;
			if(isset($maps[$m['plantform_id']])){
				$plantform_id = $maps[$m['plantform_id']];
			}
			
			$members[$i]['plantform_id'] = $plantform_id;
		}
		
		return $members;
	}
	
	/**
	 * 
	 * 导出的订单
	 * @param unknown_type $task_id
	 */
	function export_members(){
		
		$page   	= 1;
		
		$pageSize 	= PHP_INT_MAX;

        $where = array(
            'begin_time'=> '2014-02-01',
            'end_time'  => date('Y-m-d')
        );

    	$allCount = 0;
    	
		$members = $this->lists($where,$page,$pageSize,$allCount);
		
				
		//标题
		$header = explode(',', '编号,账号,登录密码,支付密码,绑定手机,绑定邮箱,邮箱密码,状态,内部帐号,最后分配时间');
		
		$data[] = $header;
	
		foreach($members as $i => $o){

			$disabled 		= ($o['disabled']) ? '禁用':'启用';
			$is_internal	= ($o['is_internal']) ? '是':'否';
			$last_use_time	= !empty($o['last_use_time']) ? $o['last_use_time']:'1970-01-01 00:00:00';
			
			
			$data[] = array(
				$o['member_id'],
				$o['member_name'],
				$o['member_pwd'],
				$o['member_pay_pwd'],
				csv_number_wrap($o['mobile_phone']),
				$o['email'],
				$o['email_pwd'],
				$disabled,
				$is_internal,
				$last_use_time
			);
		}
		
		return $data;
	}
	
	
	//是否存在
	function exist($where){

		$m = M('Member');
		return $m->table('member')
		->where("plantform_id = '{$where['plantform_id']}' AND member_name = '{$where['member_name']}'")
		->find();
	}



    /**
     * 禁用用户
     * @param $user_ids
     * @return int
     */
    function disabled_member($member_ids){

        $success_count = 0;
        
        foreach($member_ids as $uid){

            $update_rlt = $this->update($uid,array('disabled'	=> 1));

            if($update_rlt){
                $success_count++;
            }
        }

        return $success_count;
    }
}